package com.phone.handle;

import com.phone.InviteCode;
import com.phone.entity.Account;
import com.phone.entity.User;
import com.phone.entity.User2;
import com.phone.exception.*;
import com.phone.utils.TextUtil;
import com.sun.corba.se.spi.servicecontext.UEInfoServiceContext;
import com.sun.org.apache.bcel.internal.generic.ACONST_NULL;
import org.hibernate.Session;

import javax.el.ELContext;
import javax.persistence.Id;
import java.util.Date;
import java.util.List;

/**
 * Created by tisen on 2017/9/26.
 */
public class AccountHandle {
    private Session session;

    public AccountHandle(Session session) {
        this.session = session;
    }

    public void setSession(Session session) {
        this.session = session;
    }

    public Session getSession() {
        return session;
    }

    public String quick_login(String phone) throws Exception {
        List<Account> accounts = session.createQuery("from Account where phone='" + phone + "'").list();
        if (accounts == null || accounts.isEmpty())
            return quick_register(phone);
        else
            return accounts.get(0).getUser2().getId();
    }

    private String quick_register(String phone) {
        Account account = new Account();
        User2 user2 = new User2();
        user2.setNickname(phone);
        user2.setCode(InviteCode.getInviteCode());
        String userid = (String) session.save(user2);
        user2.setId(userid);
        account.setPhone(phone);
        account.setUser2(user2);
        account.setDate(new Date());
        String accountid = (String) session.save(account);
        account.setId(accountid);
        user2.setAccount(account);
        session.update(user2);
        return userid;
    }

    public String user_login(String username, String password) throws Exception {
        List<Account> users1 = session.createQuery("from Account where phone='" + username + "' and password='" + password + "'").list();
        if (users1 == null || users1.isEmpty()) {
            List<Account> users2 = session.createQuery("from Account where username='" + username + "' and password='" + password + "'").list();
            if (users2 == null || users2.isEmpty())
                throw new ErrorException(ErrorCode.ERROR_USERNAME_OR_PASSWORD);
            else
                return users2.get(0).getUser2().getId();

        } else
            return users1.get(0).getUser2().getId();
    }

    public String wx_login(String wx) throws Exception {
        List<Account> users = session.createQuery("from Account where wx='" + wx + "'").list();
        if (users == null || users.isEmpty())
            throw new ErrorException(ErrorCode.NOT_EXIST_WX_ID);
        else
            return users.get(0).getUser2().getId();
    }

    public String qq_login(String qq) throws Exception {
        List<Account> users = session.createQuery("from Account where qq='" + qq + "'").list();
        if (users == null || users.isEmpty())
            throw new ErrorException(ErrorCode.NOT_EXIST_QQ_ID);
        else
            return users.get(0).getUser2().getId();
    }

    public boolean checkPhone(String phone) throws Exception {
        List<Account> users = session.createQuery("from Account where phone='" + phone + "'").list();
        if (users == null || users.isEmpty())
            return true;
        else
            return false;
    }

    public boolean checkUsername(String username) throws Exception {
        List<Account> users = session.createQuery("from Account where username='" + username + "'").list();
        if (users == null || users.isEmpty())
            return true;
        else
            return false;
    }

    public boolean checkId(String id) throws Exception {
        User user = session.get(User.class, id);
        if (user == null) {
            return false;
        } else {
            return true;
        }
    }


    public boolean checkWX(String wx) throws Exception {
        List<Account> users = session.createQuery("from Account where wx='" + wx + "'").list();
        if (users == null || users.isEmpty())
            return true;
        else
            return false;
    }

    public boolean checkQQ(String qq) throws Exception {
        List<Account> users = session.createQuery("from Account where qq='" + qq + "'").list();
        if (users == null || users.isEmpty())
            return true;
        else
            return false;
    }

    public String phone_register(String phone, String username, String password) throws Exception {
        if (!checkPhone(phone)) {
            throw new ErrorException(ErrorCode.EXIST_PHONE);
        }
        if (!TextUtil.textIsEmpty(username) && !checkUsername(username)) {
            throw new ErrorException(ErrorCode.EXIST_USERNAME);
        }
        if (!TextUtil.textIsEmpty(username) && TextUtil.textIsEmpty(password)) {
            throw new ErrorException(ErrorCode.EMPTY_PASSWORD);
        }
        Account account = new Account();
        User2 user2 = new User2();
        account.setPhone(phone);
        account.setDate(new Date());
        if (!TextUtil.textIsEmpty(username)) {
            user2.setNickname(username);
            account.setUsername(username);
        } else {
            user2.setNickname(phone);
        }
        if (!TextUtil.textIsEmpty(password))
            account.setPassword(password);
        user2.setCode(InviteCode.getInviteCode());
        String userid = (String) session.save(user2);
        user2.setId(userid);
        account.setUser2(user2);
        String accountid = (String) session.save(account);
        account.setId(accountid);
        user2.setAccount(account);
        session.update(user2);
        return userid;
    }

    public String qq_register(String qq, String nickname, String avatar) throws Exception {
        if (!checkQQ(qq)) {
            throw new ErrorException(ErrorCode.EXIST_QQ_ID);
        } else {
            Account account = new Account();
            User2 user2 = new User2();
            user2.setCode(InviteCode.getInviteCode());
            account.setQq(qq);
            account.setDate(new Date());
            if (!TextUtil.textIsEmpty(nickname)) {
                user2.setNickname(nickname);
            }
            if (!TextUtil.textIsEmpty(avatar)) {
                user2.setAvatar(avatar);
            }
            String userid = (String) session.save(user2);
            user2.setId(userid);
            account.setUser2(user2);
            String accountid = (String) session.save(account);
            account.setId(accountid);
            user2.setAccount(account);
            session.update(user2);
            return userid;
        }
    }

    public String wx_register(String wx, String nickname, String avatar) throws Exception {
        if (!checkWX(wx)) {
            throw new ErrorException(ErrorCode.EXIST_QQ_ID);
        } else {
            User2 user2 = new User2();
            Account account = new Account();
            account.setWx(wx);
            account.setDate(new Date());
            user2.setCode(InviteCode.getInviteCode());
            if (!TextUtil.textIsEmpty(nickname)) {
                user2.setNickname(nickname);
            }
            if (!TextUtil.textIsEmpty(avatar)) {
                user2.setAvatar(avatar);
            }
            String userid = (String) session.save(user2);
            user2.setId(userid);
            account.setUser2(user2);
            String accountid = (String) session.save(account);
            account.setId(accountid);
            user2.setAccount(account);
            session.update(user2);
            return userid;
        }
    }
}
